#!/usr/bin/python2.4
#
# Copyright 2008 Google Inc. All Rights Reserved.

import unittest
from dashomatic import chart
import pygooglechart

class Chart_test(unittest.TestCase):
  def setUp(self):
    self.chart_instance = chart.Chart(title="Rohit RULES!")
    self.chart_instance.AddData([1,2,2,3],'revenue')
    self.chart_instance.AddData([20,40,30,10],'clicks')
    self.chart_instance.AddLabels(['1/1/2008','2/1/2008','3/1/2008','4/1/2008'],
                                    True)
    self.chart_instance.AddMarkerByLabel('clicks','1/1/2009',type='v',size=5,
                                color="fffacd")
    self.url = self.chart_instance.GetUrl()
  
  def testAddDumbLabelsEmpty(self):
    self.chart_instance.AddLabels([])
    self.assertEquals(self.chart_instance._labels,[])

  def testChartFromDictInvalidColor(self):
    d = {"type":'SimpleLineChart','legend':["test"],'colors':['abc']}
    self.assertRaises(pygooglechart.InvalidParametersException,chart.Chart,chart_dict=d)
    
  def testChartFromPyGoogleChart(self):
    c = pygooglechart.SimpleLineChart(200,400)
    c.set_legend(["Test Legend"])
    c2 = chart.Chart(pygooglechartobj=c)
    self.assertEquals(c2.legend,["Test%20Legend"])
    
  def testTooManyCharts(self):
    c = pygooglechart.SimpleLineChart(200,400)
    d = {"type":'SimpleLineChart','legend':"test",'colors':['000000']}
    self.assertRaises(chart.BadDataFormat,chart.Chart,pygooglechartobj=c,
                    chart_dict=d)
    
  def testChartDefaultSize(self):
    self.assertEqual(self.chart_instance.size,[400,200])

  def testChangingChartSize(self):
    self.chart_instance.size = [200,200]
    self.assertEqual(self.chart_instance.size,[200,200])
    
  def testAddDataEmptyData(self):
    self.assertRaises(chart.BadDataError,self.chart_instance.AddData,[],"a")
  
  def testAddDataBadData(self):
    self.assertRaises(chart.BadDataError,self.chart_instance.AddData,"123","a")

  def testChangingChartTitle(self):
    self.chart_instance.title = "unit testing is fun"
    self.assertEqual(self.chart_instance.title,"unit testing is fun")

  def testDeterminePadding_Zero(self):
    self.assertEqual(self.chart_instance.DeterminePadding(['']),0)
    
  def testDeterminePadding_EmptyList(self):
    self.assertRaises(ValueError,self.chart_instance.DeterminePadding,[])
  
  def testDeterminePadding_VariousLengthStrings(self):
    strings = ["a","ab","abc","abcd"]
    padding = self.chart_instance.DeterminePadding(strings)
    self.assertEquals(padding,28)
    
  def testWrongNumLabels(self):
    c = chart.Chart("SimpleLineChart")
    c.AddData([1,2,3],"test")
    self.assertRaises(chart.BadDataError,c.AddLabels,["a","b"]) 
    
  def testWrongNumberOfDataPoints(self):
    c = chart.Chart("SimpleLineChart")
    c.AddData([1,2,3],"test")
    self.assertRaises(chart.BadDataError,c.AddData,[1,2],"test") 

  def testWrongNumberOfDataPointsAfterLabels(self):
    c = chart.Chart("SimpleLineChart")
    c.AddLabels(["a","b","c"])
    self.assertRaises(chart.BadDataError,c.AddData,[1,2],"test") 
    
  def testAddDumbLabelsOne(self):
    self.chart_instance._AddDumbLabels(["hello"])
    self.assertEquals(self.chart_instance._labels,["hello"])

  def testAddDumbLabelsTwo(self):
    self.chart_instance._AddDumbLabels(["a","b"])
    self.assertEquals(self.chart_instance._labels,["a","b"])
    
  def testAddDumbLabelsThree(self):
    self.chart_instance._AddDumbLabels(["a","b","c"])
    self.assertEquals(self.chart_instance._labels,["a","b","c"])

  def testAddDumbLabelsFour(self):
    self.chart_instance._AddDumbLabels(["a","b","c","d"])
    self.assertEquals(self.chart_instance._labels,["a",'',"c","d"])
  
  def testAddDumbLabelsMany(self):
    self.chart_instance._AddDumbLabels(["a","b","c","d","e","f"])
    self.assertEquals(self.chart_instance._labels,["a",'','','d','','f'])
  
  
  def testChartObjectDataNames(self):
    self.assertEqual(len(self.chart_instance.data_names),2)

  def testModifyingData(self): 
    self.chart_instance.AddData([1,3,3,3],'clicks')
    self.chart_instance.AddData([1,2,2,2],'clicks')
    position = self.chart_instance.data_names.index('clicks')
    self.assertEqual(self.chart_instance.pre_normalized_data[position],[1,2,2,2])

  def testAddingStringData(self):
    self.chart_instance.AddData(['b','b','b','b'],'name')
    self.assertEqual(len(self.chart_instance.data_names),3)

  def testUrlGeneration(self):
    #make sure we're getting a url back
    position =  self.url.find('chart.apis.google.com')
    self.assertEqual(position,7)
    
  def testAddingNewLabelsWithDifferentSizeGraphs(self):
    self.chart_instance.size=[200,200]   
    self.chart_instance.AddLabels(
        ['1/1/2008','2/1/2008','3/1/2008','4/1/2008',
         ],True)
    small_labels = self.chart_instance.labels
    self.chart_instance.size=[1000,800]
    self.chart_instance.AddLabels(
      ['01/01/2008','02/01/2008','03/01/2008','04/01/2008'],True)
    large_labels = self.chart_instance.labels
    self.assertTrue(len(large_labels)>len(small_labels))

  def testCompareListValuesToValue(self):
    value = self.chart_instance.CompareListValuesToValue([1,2,3],3)
    self.assertEqual(value,3)
    
  def testSmartLabelsNoData(self):
    c = chart.Chart("SimpleLineChart")
    c.AddLabels(["one","two","three","four"],True)
    self.assertEquals(c._labels,[]) 

  def testSmartLabelMonthLabel(self):
    self.chart_instance.size=[900,600]
    self.chart_instance.AddLabels(['1/1/2008','2/01/2008','3/01/2008',
                                   '4/01/2008'],True)
    self.assertEqual(len(self.chart_instance.labels),14)

  def testSmartLabelQuarterlyLabel(self):
    self.chart_instance.size=[400,400]
    self.chart_instance.AddLabels(['1/1/2008','5/01/2008',
                                  '9/1/2008','12/31/208'],True)
    self.assertEqual(len(self.chart_instance.labels),4)

  def testSmartLabelAnnualLabels(self):
    self.chart_instance.size=[700,800]
    self.chart_instance.AddLabels(['1/1/2008','1/1/2009','1/1/2010',
                                   '1/1/2011'],True)
    self.assertEqual(len(self.chart_instance.labels),13)
      
  def testDictToChart_BadType(self):
    d = {"type":"blablabh"}
    self.assertRaises(Exception,chart.Chart._DictToChart,d)
  
  def testDictToChart_NoType(self):
    d = {}
    self.assertRaises(chart.BadChartError,chart.Chart._DictToChart,d)
    
  def testDictToChart_Options(self):
    d = {"type":'SimpleLineChart','legend':["test"],'colors':['000000']}
    c = chart.Chart._DictToChart(d)
    self.assertEquals(c.legend,["test"])
    self.assertEquals(c.colours,['000000'])
    
  def testAddMarker(self):
    self.chart_instance.AddMarker(0,0)
    self.assertEquals(len(self.chart_instance.chart.markers),1)
  
  def testAddMarkerByLabel(self):
    self.chart_instance.AddMarkerByLabel('1/1/2008',0)
    self.assertEquals(len(self.chart_instance.chart.markers),0)

  def testAddMarkerByLabelNoMarker(self):
    self.chart_instance.AddMarkerByLabel('1/1/20100',0)
    self.assertEquals(len(self.chart_instance.chart.markers),0)

  def testAddMarkerByLabelNoMarkerStrict(self):
    self.assertRaises(ValueError,self.chart_instance.AddMarkerByLabel,
                      "revenue",'1/1/2010',strict=True)
    


if __name__ == '__main__':
  tests = []
  tests.append(unittest.TestLoader().loadTestsFromTestCase(Chart_test))
  all_tests = unittest.TestSuite(tests)
  unittest.TextTestRunner(verbosity=2).run(all_tests)
